package com.iwhalecloud.bss.kite.cucc.web.controller.scene;

import com.iwhalecloud.bss.kite.client.api.cust.query.IQueryAccountService;
import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneProdInstService;
import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.transference.TransferenceVO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.ExtParamConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr.IPersonAttrDealService;
import com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module.LocalTransferencePositionCustModuleHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.transaction.KiteTransactional;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.controller.scene.ProdAttrController;
import com.iwhalecloud.bss.kite.web.service.scene.ISceneService;
import com.ztesoft.bss.common.util.SpringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <Description> 过户 <br>
 *
 * @author wei.yanghong<br>
 * @version 1.0<br>
 * @see com.iwhalecloud.bss.kite.web.controller.scene <br>
 * @since R9.0<br>
 */
@RestController("cuccTransference")
@RequestMapping("/cuccTransference")
@Api(tags = {"/cuccTransference - 过户"})
public class TransferenceController {

    @Autowired
    private IOperateSceneProdInstService operateSceneProdInstService;

    @Autowired
    private IOperateSceneService operateSceneService;

    @Autowired
    private ISceneService sceneService;

    @Autowired
    private IQueryAccountService queryAccountService;

    @Autowired
    private ProdAttrController prodAttrController;

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IPersonAttrDealService personAttrDealService;

    @KiteTransactional
    @ApiOperation(value = "修改拥有者", notes = "修改拥有者")
    @ApiImplicitParams({@ApiImplicitParam(name = "newCustId", value = "目标客户标识", required = true),
        @ApiImplicitParam(name = "newCustName", value = "目标客户名称", required = true)
    })
    @PutMapping(value = "/ownerCustId")
    public DataResult<TransferenceVO> setNewOwnerCustId(@RequestParam String sceneInstId,
        @RequestParam String newCustId, @RequestParam String newCustName) {
        //暂时注释，需要新增参数moduleId
        //        personAttrDealService.apply(KiteWebContext.getContext().getCurrentSceneInstId(), "custName", newCustId, newCustName);

        SpringUtil.getBean(IOperateSceneInstService.class)
            .putExtParameter(KiteWebContext.getContext().getCurrentSceneInstId(),
                ExtParamConsts.EXT_PARAM_UPDATE_SCENE_FALG, KeyConsts.TAG_FALSE_FAIL_NO_FAIL_0);

        if (KiteStringUtils.isEmpty(sceneInstId)) {
            sceneInstId = KiteWebContext.getContext().getCurrentSceneInstId();
        }
        operateSceneService.setNewOwnerCustId(sceneInstId, newCustId, newCustName);

        LocalTransferencePositionCustModuleHandler mTransferenceTCustInfoListModuleHandler = SpringUtil
            .getBean(LocalTransferencePositionCustModuleHandler.class);
        TransferenceVO transferenceVOs = mTransferenceTCustInfoListModuleHandler
            .getTransferDisplayInfoAttrs(KiteWebContext.getContext().getCurrentSceneInstId());
        //重新定位客户的时候，把账务定制关系还原会老客户的账务定制关系
        operateSceneService.setNewAcctId(sceneInstId, "", "");
        return ErrorConsts.SUCCESS.getResult(transferenceVOs);
    }

    @KiteTransactional
    @ApiOperation(value = "修改账户", notes = "修改账户")
    @ApiImplicitParams({@ApiImplicitParam(name = "acctId", value = "目标客户账户标识", required = true),
        @ApiImplicitParam(name = "acctDesc", value = "目标客户账户描述", required = true)
    })
    @PutMapping(value = "/account")
    public DataResult<ModuleVO> setNewAcctId(@RequestParam String acctId, @RequestParam String acctDesc) {
        //       暂时注释，需要新增参数moduleId
        //        personAttrDealService.apply(KiteWebContext.getContext().getCurrentSceneInstId(), "acctName", acctId, acctDesc);

        operateSceneService.setNewAcctId(KiteWebContext.getContext().getCurrentSceneInstId(), acctId, acctDesc);
        LocalTransferencePositionCustModuleHandler mTransferenceTCustInfoListModuleHandler = SpringUtil
            .getBean(LocalTransferencePositionCustModuleHandler.class);
        TransferenceVO transferenceVOs = mTransferenceTCustInfoListModuleHandler
            .getTransferDisplayInfoAttrs(KiteWebContext.getContext().getCurrentSceneInstId());
        return ErrorConsts.SUCCESS.getResult(transferenceVOs);
    }

    @KiteTransactional
    @ApiOperation(value = "更新模块信息", notes = "更新模块信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "fieldName", value = "属性规格编码", required = true),
        @ApiImplicitParam(name = "value", value = "修改值", required = true),
        @ApiImplicitParam(name = "valueDesc", value = "展示值", required = true)
    })
    @PutMapping(value = "/updateCommonAttr")
    public DataResult<ModuleVO> updateCommonAttr(@RequestParam String fieldName, @RequestParam String value,
        @RequestParam String valueDesc, @RequestParam(required = false) String moduleId) {
        //暂时注释，需要新增参数moduleId
        // personAttrDealService.apply(KiteWebContext.getContext().getCurrentSceneInstId(), fieldName, value, valueDesc);
        //场景回刷
        TransferenceVO transferenceVOs = new TransferenceVO();
        if (KiteStringUtils.isNotEmpty(moduleId)) {

        }
        else {
            LocalTransferencePositionCustModuleHandler mTransferenceTCustInfoListModuleHandler = SpringUtil
                .getBean(LocalTransferencePositionCustModuleHandler.class);
            transferenceVOs = mTransferenceTCustInfoListModuleHandler
                .getTransferDisplayInfoAttrs(KiteWebContext.getContext().getCurrentSceneInstId());
        }
        return ErrorConsts.SUCCESS.getResult(transferenceVOs);
    }

}
